package com.louis.kitty.admin.controller;

import java.awt.image.BufferedImage;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.louis.kitty.admin.model.LolCc;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import com.google.code.kaptcha.Constants;
import com.google.code.kaptcha.Producer;
import com.louis.kitty.admin.model.SysUser;
import com.louis.kitty.admin.security.JwtAuthenticatioToken;
import com.louis.kitty.admin.sevice.SysUserService;
import com.louis.kitty.admin.util.PasswordUtils;
import com.louis.kitty.admin.util.SecurityUtils;
import com.louis.kitty.admin.vo.LoginBean;
import com.louis.kitty.common.utils.IOUtils;
import com.louis.kitty.core.http.HttpResult;

/**
 * 登录控制器
 *
 * @author Louis
 * @date Oct 29, 2018
 */
@RestController
public class SysLoginController {

    @Autowired
    private Producer producer;
    @Autowired
    private SysUserService sysUserService;
    @Autowired
    private AuthenticationManager authenticationManager;

    private int me = 0;

    @GetMapping("captcha.jpg")
    public void captcha(HttpServletResponse response, HttpServletRequest request) throws ServletException, IOException {
        response.setHeader("Cache-Control", "no-store, no-cache");
        response.setContentType("image/jpeg");

        // 生成文字验证码
        String text = producer.createText();
        // 生成图片验证码
        BufferedImage image = producer.createImage(text);
        // 保存到验证码到 session
        request.getSession().setAttribute(Constants.KAPTCHA_SESSION_KEY, text);

        ServletOutputStream out = response.getOutputStream();
        ImageIO.write(image, "jpg", out);
        IOUtils.closeQuietly(out);
    }

    /**
     * 登录接口
     */
    @PostMapping(value = "/login")
    public HttpResult login(@RequestBody LoginBean loginBean, HttpServletRequest request) throws IOException {
        String username = loginBean.getAccount();
        String password = loginBean.getPassword();
        String captcha = loginBean.getCaptcha();

        // 从session中获取之前保存的验证码跟前台传来的验证码进行匹配
        Object kaptcha = request.getSession().getAttribute(Constants.KAPTCHA_SESSION_KEY);
		/*if(kaptcha == null){
			return HttpResult.error("验证码已失效");
		}*/
//		if(!captcha.equals(kaptcha)){
//			return HttpResult.error("验证码不正确");
//		}

        // 用户信息
        SysUser user = sysUserService.findByName(username);

        // 账号不存在、密码错误
        if (user == null) {
            return HttpResult.error("账号不存在");
        }

        if (!PasswordUtils.matches(user.getSalt(), password, user.getPassword())) {
            return HttpResult.error("密码不正确");
        }

        // 账号锁定
        if (user.getStatus() == 0) {
            return HttpResult.error("账号已被锁定,请联系管理员");
        }

        // 系统登录认证
        JwtAuthenticatioToken token = SecurityUtils.login(request, username, password, authenticationManager);

        return HttpResult.ok(token);
    }

    @PostMapping(value = "/UserApi")
    public LolCc UserApi() throws ServletException, IOException {
        LolCc lolCc = new LolCc();
        if (me == 1) {
            me = 2;
			System.out.println("2");
            lolCc.setData("28D2DEF2951D60D91F5136B8492F8C9330E3BD4440406E7AD49EF8CF30FCA7A6B35E4D98FE2600F2BD245928C11B5F159ED153EDE2D7011DB1EA12D3F04B2834E5E82CA17919F52E6064A0BCC35CE0219D94ABC1C4E6F3FB327845E2BB087E03BBCDD6BD8750AB4A14065849F18B7E42800044533218D90111E0BA42531ACA860E37C58FA7C532C70BFF73FFF42F3E6448FF5D635E24566523F7B41F5B773EE77BF51F88C1D63AD3A3660D8279F927DD5A38A53A1CB4B08A12816225E85575583B70F16F0A426B138181A4CEC95F9908542150FE0A42F78A7203962E791278A234364060A19358DC36D6DB4D8C8CB57086BA5DFCFFD208C238F4403601959A5");
        } else if (me == 2) {
            me = 0;
            lolCc.setData("28D2DCED811126C1144F21E70D7C828B71391EE3DEC996EF505A239D75ACB69BB7565BCEA57D49E6B6245926C7064449DAC65DFCE1D24D");
            System.out.println("1");
        } else {
            me = 1;
			System.out.println("0");
            lolCc.setData("28D2DDEB8B106D95422E6CEE1B399A807FAEBA4E061F7D6CC7D7A2D22AE9E0C8F880DD7078AB967745ACCAB05515515E809C1DA6B3C40A11AAFF1E8DB2006B66A5AF53F77528FD6D333AAEA4930EB279DBD5D19B99B191B0727A04F9BF473752A2E5DFEF9F58AD481B024552A8F12C1AD01B4F5D27599A5D45BBC214055DC49E534AB6D8F3A579854DFB4FFDD421286A48FF4E6D5073564523F7B07C05316EE963A73E98979C7BD0BD7B52C36FA474815C3AA96748C9FDDE0D9C7064AB126A0C6D41F17A062923138F8FF3CAC453881E6D2F59EB0A42F4936744C67B25422CE265451C28E6C51B9C7FD69718D2DDA96C8DEA2EBAECC61BD27D");
        }
        lolCc.setMsg("");
        lolCc.setStatus("1");
        lolCc.setToken("2c8ca6ad-b61e-433f-aeca-db0d9ed001b2");
        return lolCc;
    }

}
